/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.seminariosab.srvlts;

import com.seminariosab.daos.SABEntyDao;
import com.seminariosab.entys.SABRepresentanteEnty;
import com.seminariosab.tools.SABConstantine;
import com.seminariosab.tools.SABSeedRamdomize;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Administrador
 */
public class DoLogin extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        //txtusuario-txtpwd
        String ucredential[] = null;
        String URL_REDIR="/login.jsp";
        try {
            ucredential=new String[]{null,null};
            ucredential[0]=new String(request.getParameter("txtusuario"));
            ucredential[1]=new String(request.getParameter("txtpwd"));
        } catch (Exception e) {
            ucredential=null;
        }
        
        try {
            /*
             * TODO output your page here. You may use following sample code.
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet DoLogin</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet DoLogin at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");*/
            
            if(ucredential.length>0){
                SABRepresentanteEnty ent=null;
                try {
                    ent=(new SABEntyDao().getRepresentante(ucredential[0], ucredential[1]));                    
                    String webuser=SABConstantine.SABWebOpeObject;
                    HttpSession ses=request.getSession(true);
                    ses.setAttribute(webuser,ent);
                    //request.getRequestDispatcher(SABConstantine.SABURLSuccesLog).forward(request, response);
                    response.sendRedirect(SABConstantine.SABURLSuccesLog);
                } catch (Exception e) {
                    response.sendRedirect(SABConstantine.SABURLinit);
                }
            }else{
                response.sendRedirect(SABConstantine.SABURLinit);
            }
            
        }
        catch(Exception logex){
            response.sendRedirect(SABConstantine.SABURLinit);
        }
        finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "DoLogin";
    }// </editor-fold>
}
